package cn.msqweb.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.ProdTag;
import cn.msqweb.model.Result;
import cn.msqweb.service.ProdTagService;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分组标签控制层
 */
@Api(tags = "商品分组标签接口管理")
@RequestMapping("prod/prodTag")
@RestController
public class ProdTagController {

    @Autowired
    private ProdTagService prodTagService;


    @ApiOperation("多条件分页查询商品分组标签")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<Page<ProdTag>> loadProdTagPage(@ApiParam("页码") @RequestParam Long current,
                                                 @ApiParam("每页显示条数") @RequestParam Long size,
                                                 @ApiParam("标签名称") @RequestParam(required = false) String title,
                                                 @ApiParam("状态") @RequestParam(required = false) Integer status) {
        // 创建分页对象
        Page<ProdTag> page = new Page<>(current,size);
        // 多条件分页查询
        page = prodTagService.page(page,new LambdaQueryWrapper<ProdTag>()
                // 如果like和eq同时存在，先写eq条件
                .eq(ObjectUtil.isNotNull(status),ProdTag::getStatus,status)
                .like(StringUtils.hasText(title),ProdTag::getTitle,title)
                .orderByDesc(ProdTag::getSeq)
        );
        return Result.success(page);
    }

    @ApiOperation("新增商品分组标签")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prodTag:save')")
    public Result<String> saveProdTag(@ApiParam("商品分组标签对象") @RequestBody ProdTag prodTag) {
        boolean saved = prodTagService.save(prodTag);
        return Result.handler(saved);
    }

    @ApiOperation("根据标识查询商品分组标签信息")
    @GetMapping("info/{tagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:info')")
    public Result<ProdTag> loadProdTagInfo(@ApiParam("分组标签标识") @PathVariable Long tagId) {
        ProdTag prodTag = prodTagService.getById(tagId);
        return Result.success(prodTag);
    }

    @ApiOperation("修改分组标签信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodTag:update')")
    public Result<String> modifyProdTagInfo(@ApiParam("分组标签对象") @RequestBody ProdTag prodTag) {
        boolean updated = prodTagService.updateById(prodTag);
        return Result.handler(updated);
    }

    @ApiOperation("删除商品分组标签")
    @DeleteMapping("{tagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public Result<String> removeProdTag(@ApiParam("分组标签标识") @PathVariable Long tagId) {
        boolean removed = prodTagService.removeById(tagId);
        return Result.handler(removed);
    }

    @ApiOperation("查询状态正常的分组标签集合")
    @GetMapping("listTagList")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<List<ProdTag>> loadProdTagList() {
        List<ProdTag> list = prodTagService.queryProdTagList();
        return Result.success(list);
    }
}
